using UnityEngine;
using System;
using System.Collections.Generic;

public class UIManager : MonoBehaviour
{
    public static UIManager Instance { private set; get; }

    public GameObject MainPageBtn;
    public GameObject DongfuPage;
    public GameObject CharacterPage;
    public GameObject ZongmenPage;
    public GameObject XiuXingPage;
    public GameObject SplashPage;
    public GameObject battleSecne;
    public GameObject jieyin;
    public GameObject gonggao;

    private List<string> m_showingPages = new List<string>();

    private Dictionary<string, GameObject> m_pageMap = new Dictionary<string, GameObject>();

    public void Awake()
    {
        Instance = this;
    }

    public void Init()
    {
        m_pageMap.Add(UINames.MainPage, MainPageBtn);
        m_pageMap.Add(UINames.DongfuPage, DongfuPage);
        m_pageMap.Add(UINames.CharacterPage, CharacterPage);
        m_pageMap.Add(UINames.ZongmenPage, ZongmenPage);
        m_pageMap.Add(UINames.XiuXingPage, XiuXingPage);
        m_pageMap.Add(UINames.SplashPage, SplashPage);
        m_pageMap.Add(UINames.BattleScene, battleSecne);
        m_pageMap.Add(UINames.GongGao, gonggao);
        m_pageMap.Add(UINames.JieYin, jieyin);


        foreach(var go in m_pageMap.Values)
        {
            go.SetActive(false);
        }
    }

    public void ShowPage(params string[] pageNames)
    {
        foreach (var name in m_showingPages)
        {
            DoHideUI(name);
        }
        m_showingPages.Clear();

        foreach (var name in pageNames)
        {
            DoShowUI(name);
            m_showingPages.Add(name);
        }
    }

    public void Switch2Default()
    {
        ShowPage(UINames.DongfuPage);
        DoShowUI(UINames.MainPage);
    }

    private void DoShowUI(string name)
    {
        GameObject page = null;
        if (m_pageMap.TryGetValue(name, out page))
        {
            page.SetActive(true);
            page.SendMessage("OnShow", null, SendMessageOptions.DontRequireReceiver);
        }
    }

    private void DoHideUI(string name)
    {
        GameObject page = null;
        if (m_pageMap.TryGetValue(name, out page))
        {
            page.SetActive(false);
            page.SendMessage("OnHide", null, SendMessageOptions.DontRequireReceiver);
        }
    }


}